package com.tencent.qcloud.timchat.utils;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.bumptech.glide.Glide;
import com.wbao.dianniu.R;
import com.wbao.dianniu.utils.DateUtils;
import com.wbao.dianniu.utils.GlideCircleTransform;
import com.wbao.dianniu.utils.PictureDownloadUtils;
import com.wbao.dianniu.utils.Utils;

import java.util.List;

/**
 * 会话界面adapter
 */
public class ConversationAdapter extends ArrayAdapter<Conversation> implements View.OnClickListener {
    private String TAG = ConversationAdapter.class.getSimpleName();
    private int resourceId;
    //    private View view;
//    private ViewHolder viewHolder;
    private OnItemClickListener onItemClickListener;
    private Context mContext;

    /**
     * Constructor
     *
     * @param context  The current context.
     * @param resource The resource ID for a layout file containing a TextView to use when
     *                 instantiating views.
     * @param objects  The objects to represent in the ListView.
     */
    public ConversationAdapter(Context context, int resource, List<Conversation> objects) {
        super(context, resource, objects);
        mContext = context;
        resourceId = resource;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = new ViewHolder();
        if (convertView != null) {
            viewHolder = (ViewHolder) convertView.getTag();
        } else {
            convertView = LayoutInflater.from(mContext).inflate(resourceId, null);
            viewHolder = new ViewHolder();
            viewHolder.headIV = (ImageView) convertView.findViewById(R.id.head);
            viewHolder.unReadTV = (TextView) convertView.findViewById(R.id.unread);
            viewHolder.nameTV = (TextView) convertView.findViewById(R.id.name);
            viewHolder.atMsgTV = (TextView) convertView.findViewById(R.id.at_msg_notify);
            viewHolder.contentTV = (TextView) convertView.findViewById(R.id.content);
            viewHolder.timeTV = (TextView) convertView.findViewById(R.id.time);
            viewHolder.layout = (LinearLayout) convertView.findViewById(R.id.conversation_item_layout);
            viewHolder.topBtn = (Button) convertView.findViewById(R.id.btnTop);
            viewHolder.deleteBtn = (Button) convertView.findViewById(R.id.btnDelete);
            convertView.setTag(viewHolder);
        }
        final Conversation data = getItem(position);
//        Log.e(TAG, data.getUrl() + "   name:" + data.getName() + "  indef:" + data.getIdentify());
        displayDefaultHeadImg(mContext, viewHolder.headIV, data.getUrl());
        if (TextUtils.isEmpty(data.getName())) {
            String name = data.getIdentify().replace("qa", "").replace("prod", "");
            viewHolder.nameTV.setText(name);
        } else {
            String name = data.getName();
            if (data.getName().equals(data.getIdentify())) {
                name = name.replace("qa", "").replace("prod", "");
            }
            viewHolder.nameTV.setText(name);
        }

        viewHolder.contentTV.setText(data.getLastMessageSummary());
        viewHolder.timeTV.setText(DateUtils.getTimeStr(data.getLastMessageTime()));
        long unRead = data.getUnreadNum();
        if (unRead <= 0) {
            viewHolder.unReadTV.setVisibility(View.INVISIBLE);
        } else {
            viewHolder.unReadTV.setVisibility(View.VISIBLE);
            String unReadStr = String.valueOf(unRead);
            if (unRead > 99) {
                unReadStr = mContext.getResources().getString(R.string.time_more);
            }
            viewHolder.unReadTV.setText(unReadStr);
        }
        viewHolder.deleteBtn.setOnClickListener(this);
        viewHolder.topBtn.setOnClickListener(this);
        viewHolder.layout.setOnClickListener(this);
        viewHolder.deleteBtn.setTag(R.id.btnDelete, position);
        viewHolder.topBtn.setTag(R.id.btnTop, position);
        viewHolder.layout.setTag(R.id.conversation_item_layout, position);
        return convertView;
    }

    private void displayDefaultHeadImg(Context context, ImageView imageView, String imgUrl) {
        if (null != context) {
            Glide.with(context)
                    .load(imgUrl)
                    .centerCrop()
                    .placeholder(R.drawable.default_head_icon)
                    .crossFade()
                    .override(Utils.dip2px(context, 55), Utils.dip2px(context, 55))
                    .bitmapTransform(new GlideCircleTransform(context))
                    .thumbnail(0.2f)
                    .into(imageView);
        }
    }

    @Override
    public void onClick(View v) {
        if (null != onItemClickListener)
            onItemClickListener.itemClick(v);
    }

    public class ViewHolder {
        public ImageView headIV;
        public TextView unReadTV;
        public TextView nameTV;
        public TextView atMsgTV;
        public TextView contentTV;
        public TextView timeTV;
        public LinearLayout layout;
        public Button deleteBtn;
        public Button topBtn;
    }

    /**
     * 设置回调监听
     *
     * @param listener
     */
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    public interface OnItemClickListener {
        void itemClick(View view);
    }


}
